package com.example.yuejianapp14.Activity.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yuejianapp14.Activity.JavaBean.Success;
import com.example.yuejianapp14.Activity.JavaBean.User;
import com.example.yuejianapp14.Activity.Listener.ClubNoListListener;
import com.example.yuejianapp14.Activity.Listener.ListListener;
import com.example.yuejianapp14.Activity.Model.UserModel;
import com.example.yuejianapp14.R;
import com.squareup.picasso.Picasso;

public class UpdateUserActivity extends AppCompatActivity {
    private Button user_back,user_update;
    private EditText user_name,user_phone,user_password,user_address,user_info;
    private ImageView user_pic;
    private Context mContext;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user);
        initViews();
        getData();
        user_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        user_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel userModel=new UserModel();
                userModel.updateuser(sp.getString("uid",""),user_name.getText().toString(),
                        user_phone.getText().toString(),user_password.getText().toString(),
                        user_address.getText().toString(),user_info.getText().toString(),updateuserListener);
            }
        });
    }
    public void initViews() {
//        recyclerView = (RecyclerView)findViewById(R.id.rv_user_tiezi);
//        recyclerView.setLayoutManager(new LinearLayoutManager((mContext)));
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
        sp=getSharedPreferences("uid",MODE_PRIVATE);
        user_back = (Button) findViewById(R.id.user_back);
        user_name =(EditText) findViewById(R.id.user_name) ;
        user_phone =(EditText) findViewById(R.id.user_phone) ;
        user_password =(EditText) findViewById(R.id.user_password) ;
        user_address =(EditText) findViewById(R.id.user_address) ;
        user_info =(EditText) findViewById(R.id.user_info) ;
        user_pic=(ImageView) findViewById(R.id.user_pic);
        user_update=(Button)findViewById(R.id.user_update);
    }
    ClubNoListListener<User> UserInfoListener=new ClubNoListListener<User>() {
        public void onResponse(User userInfo) {
            user_name.setText(userInfo.getUname());
            user_phone.setText(userInfo.getUphone());
            user_password.setText(userInfo.getUpassword());
            user_address.setText(userInfo.getUaddress());
            user_info.setText(userInfo.getUintro());
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/User/"+userInfo.getUpicture()).into(user_pic);
        }
        @Override
        public void onFail(String msg) {
        }
    };
    ListListener<Success> updateuserListener=new ListListener<Success>() {
        @Override
        public void onResponse(Success a) {
            if(a.getSuccess().equals("1")){
                Toast.makeText(UpdateUserActivity.this, "修改成功",Toast.LENGTH_SHORT).show();
                finish();
            }
            else {
                Toast.makeText(UpdateUserActivity.this, "修改失败",Toast.LENGTH_SHORT).show();
                finish();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(UpdateUserActivity.this, "fail",Toast.LENGTH_SHORT).show();
        }
    };
    private void getData() {
        Intent intent = getIntent();
        UserModel userModel=new UserModel();
        userModel.findUserinfo(sp.getString("uid",""),UserInfoListener);
    }
}
